"use strict";
cc._RF.push(module, '6555a6H+OZK1ob5A9TuGYhl', 'startGameitem');
// Script/startGameitem.js

"use strict";

var _XmlHttpTool = require("./XmlHttpTool");

var _XmlHttpTool2 = _interopRequireDefault(_XmlHttpTool);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

cc.Class({

    extends: cc.Component,
    properties: {

        shopImageUrl: cc.Sprite,
        shopName: cc.Label,
        shopStatus: cc.Integer,
        chargeBtn: cc.Button,
        shopStar: cc.Label,
        goodDicjson: null,
        staricon: cc.Sprite,
        alreadyCharge: cc.Node
    },

    onLoad: function onLoad() {

        var clickEventHandler = new cc.Component.EventHandler();
        clickEventHandler.target = this.node; //这个 node 节点是你的事件处理代码组件所属的节点
        clickEventHandler.component = "startGameitem"; //这个是代码文件名
        clickEventHandler.handler = "chargegoTo";
        this.chargeBtn.clickEvents.push(clickEventHandler);
    },


    chargegoTo: function chargegoTo() {

        console.log("兑换商品");
        var changebig2 = cc.scaleTo(0.2, 1.1);

        var finish = cc.callFunc(function () {

            var changesmail2 = cc.scaleTo(0.1, 1.0);
            this.chargeBtn.node.runAction(changesmail2);
        }, this);

        var seq = cc.sequence(changebig2, finish);

        this.chargeBtn.node.runAction(seq);

        var that = this;

        _XmlHttpTool2.default.NhttpPost("/marmot/prize_exchange", { "tid": that.goodDicjson["id"] }, function (res) {

            cc.find("Canvas/MainCamera/shopAllView").active = false;
            var cranNode = cc.find("Canvas/MainCamera/addressAllView");
            console.log("兑换成功", res);
            cc.find("Canvas/MainCamera/addressAllView/AddressView/LocationView").getComponent("AddressView").init(that.goodDicjson["id"], res, false);
            cranNode.active = true;
        }, function (failres) {

            var failecranNode = cc.find("Canvas/MainCamera/faileAlertView");
            failecranNode.active = true;
            console.log("兑换失败", failres);
        });
    },

    init: function init(goodDic) {

        this.goodDicjson = goodDic;
        this.shopName.getComponent(cc.Label).string = goodDic["name"];
        this.shopStar.getComponent(cc.Label).string = "x" + goodDic["quantity"];

        var that = this;

        cc.loader.load({ url: goodDic["thumbnail"], type: 'png' }, function (err, img) {

            var mylogo = new cc.SpriteFrame(img);

            that.shopImageUrl.getComponent(cc.Sprite).spriteFrame = mylogo;
        });

        if (goodDic["quantity"] < 0) {

            cc.loader.loadRes("已兑完星币", cc.SpriteFrame, function (err, spriteFrame) {
                that.staricon.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            });
            this.alreadyCharge.active = true;
            this.chargeBtn.node.active = false;
        } else {

            this.alreadyCharge.active = false;
            this.chargeBtn.node.active = true;
        }
    }

});

cc._RF.pop();